﻿#Region "IMPORTS"

Imports NHibernate
Imports NHibernate.Expression

#End Region

Public MustInherit Class DAO(Of MODELO As IModelo)
    Implements IDAO(Of MODELO)



#Region "METODOS CRUD"

    Public Overridable Sub Delete(ByVal modelo As MODELO) Implements IDAO(Of MODELO).Delete
        Try
            Me.ValidarDelete(modelo)
            Using session As ISession = SessionFactory.OpenSession()
                Using transaction As ITransaction = session.BeginTransaction()
                    session.Delete(modelo)
                    transaction.Commit()
                End Using
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Overridable Function Insert(ByVal modelo As MODELO) As MODELO Implements IDAO(Of MODELO).Insert
        Try
            Me.ValidarInsert(modelo)
            Using session As ISession = SessionFactory.OpenSession()
                Using transaction As ITransaction = session.BeginTransaction()
                    session.Save(modelo)
                    transaction.Commit()
                    Return (modelo)
                End Using
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Overridable Sub Update(ByVal modelo As MODELO) Implements IDAO(Of MODELO).Update

        Try
            Me.ValidarUpdate(modelo)
            Using session As ISession = SessionFactory.OpenSession()
                Using transaction As ITransaction = session.BeginTransaction()
                    session.Update(modelo)
                    transaction.Commit()
                End Using
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Overridable Function GetById(ByVal id As Integer) As MODELO Implements IDAO(Of MODELO).GetById
        Try
            Using session As ISession = SessionFactory.OpenSession()
                Return session.[Get](Of MODELO)(id)
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Function


    Public Overridable Function GetAll() As IList(Of MODELO) Implements IDAO(Of MODELO).GetAll

        Try
            Using session As ISession = SessionFactory.OpenSession()
                Dim list As List(Of MODELO) = session.CreateCriteria(GetType(MODELO)).List(Of MODELO)()
                list.Reverse()
                Return list
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Function


    Public Overridable Function Find(ByVal NHQL As String) As IList(Of MODELO) Implements IDAO(Of MODELO).Find

        Try
            Using session As ISession = SessionFactory.OpenSession()
                Dim list As List(Of MODELO) = session.CreateQuery(NHQL).List(Of MODELO)()
                list.Reverse()
                Return list
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Overridable Function Search(ByVal condicoes As IList(Of Condicao)) As IList(Of MODELO) Implements IDAO(Of MODELO).Search

        Try
            If condicoes.Count = 0 Then
                Return Me.GetAll
            Else
                Dim NHQL As String = New ConverteNHQL().Converte(condicoes, GetType(MODELO).FullName, GetType(MODELO).Name)
                Return Me.Find(NHQL)
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Function

#End Region

#Region "ASSINATURA MEDODOS ABSTRATOS"

    Protected MustOverride Sub ValidarInsert(modelo As MODELO) Implements IDAO(Of MODELO).ValidarInsert

    Protected MustOverride Sub ValidarDelete(modelo As MODELO) Implements IDAO(Of MODELO).ValidarDelete

    Protected MustOverride Sub ValidarUpdate(modelo As MODELO) Implements IDAO(Of MODELO).ValidarUpdate

#End Region


    Public Function FindUnique(NHQL As String) As Object Implements IDAO(Of MODELO).FindUnique

        Try
            Using session As ISession = SessionFactory.OpenSession()
             Dim result As NHibernate.IQuery = session.CreateQuery(NHQL)
                Return result.UniqueResult 
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Function

End Class
